package com.info.zhiduoduo.weixin.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.project.ProjectType;
import com.info.zhiduoduo.common.constants.system.UserType;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.NumberUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.entity.company.CompanyAccountflow;
import com.info.zhiduoduo.repository.entity.maker.MakerAccountflow;
import com.info.zhiduoduo.repository.mapper.order.OrderMapper;
import com.info.zhiduoduo.weixin.formbean.order.FlowListFormBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService extends BaseService {

	@Autowired
	OrderMapper orderMapper;

	/**
	 获取交易流水
	 inout 0:全部 1:收入 2:支出

	 @param formBean
	 @return
	 */
	public SmartResult getFlowList(BaseSimpleFormBean<FlowListFormBean> formBean) {

		int userid = formBean.getUserid();
		int idtype = formBean.getIdtype();
		FlowListFormBean reqdata = formBean.getReqdata();

		int flowtype = 0;
		if (reqdata != null) flowtype = reqdata.getFlowtype();

		if (idtype == UserType.MAKER) {
			Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
			Wrapper wrapper = Condition.create().eq("userid", userid)
									   .notIn("project_type", ProjectType.entrustProjectTypes())
									   .orderBy("id", false);
			if (flowtype > 0) wrapper.eq("flowtype", flowtype);
			List<MakerAccountflow> list = new MakerAccountflow().selectList(wrapper);
			return smartResult(list, page.getTotal(), page.getPages(), page.getPageNum());
		} else {
			Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
			Wrapper wrapper = Condition.create().eq("compid", userid).orderBy("id", false);
			if (flowtype > 0) wrapper.eq("flowtype", flowtype);
			List<CompanyAccountflow> list = new CompanyAccountflow().selectList(wrapper);
			return smartResult(list, page.getTotal(), page.getPages(), page.getPageNum());
		}

	}

	/**
	 获取交易流水详情

	 @param formBean
	 @return
	 */
	public SmartResult getFlowDetail(BaseSimpleFormBean<Integer> formBean) {
		int userid = formBean.getUserid();
		Integer flowid = formBean.getReqdata();
		int idtype = formBean.getIdtype();
		if (NumberUtil.isAnyZero(userid, flowid)) {
			return errorParamSmartResult();
		}

		if (idtype == UserType.MAKER) {
			return smartResult(new MakerAccountflow().selectById(flowid));
		} else {
			return smartResult(new CompanyAccountflow().selectById(flowid));
		}

	}
}
